<?php
namespace api\modules\v1\models;

use api\components\ApiModel;
use common\models\AROrder;
use common\models\AROrderTime;
use common\models\ARUserScoring;
use Yii;
use common\models\ARBrokerScoring;

/**
 * 响应时间模型
 * @author CGA
 */
class ReplyTime extends ApiModel
{
    /**
     * 更新订单经纪人响应时间
     * @param $order_id 订单编号
     */
    public static function updateOrderBrokerTime($order_id)
    {
        $orderTime = AROrderTime::find()->where(['order_id' => $order_id])->one();
        $orderTime->broker_confirm = Yii::$app->formatter->asTimestamp('now');
        return $orderTime->update() ? true : false;
    }

    /**
     * 更新订单需求方响应时间
     * @param $order_id 订单编号
     */
    public static function updateOrderDemanderTime($order_id)
    {
        $orderTime = AROrderTime::find()->where(['order_id' => $order_id])->one();
        $orderTime->demander_confirm = Yii::$app->formatter->asTimestamp('now');
        return $orderTime->update() ? true : false;
    }

    /**
     * 更新订单供应方响应时间
     * @param $order_id 订单编号
     */
    public static function updateOrderSupplierTime($order_id)
    {
        $orderTime = AROrderTime::find()->where(['order_id' => $order_id])->one();
        $orderTime->supplier_confirm = Yii::$app->formatter->asTimestamp('now');
        return $orderTime->update() ? true : false;
    }

    /**
     * 更新买方转款时间
     * @param $order_id 订单编号
     */
    public static function updateOrderBuyerTransfer($order_id)
    {
        $orderTime = AROrderTime::find()->where(['order_id' => $order_id])->one();
        $orderTime->user_transfer = Yii::$app->formatter->asTimestamp('now');
        return $orderTime->update() ? true : false;
    }

    /**
     * 更新订单经纪人收款时间
     * @param $order_id 订单编号
     */
    public static function updateOrderBrokerReceipt($order_id)
    {
        $orderTime = AROrderTime::find()->where(['order_id' => $order_id])->one();
        $orderTime->broker_receipt = Yii::$app->formatter->asTimestamp('now');
        return $orderTime->update() ? true : false;
    }

    /**
     * 更新订单经纪人转款时间
     * @param $order_id 订单编号
     */
    public static function updateOrderBrokerTransfer($order_id)
    {
        $orderTime = AROrderTime::find()->where(['order_id' => $order_id])->one();
        $orderTime->broker_transfer = Yii::$app->formatter->asTimestamp('now');
        return $orderTime->update() ? true : false;
    }

    /**
     * 更新卖方收款时间
     * @param $order_id 订单编号
     */
    public static function updateOrderSellerReceipt($order_id)
    {
        $orderTime = AROrderTime::find()->where(['order_id' => $order_id])->one();
        $orderTime->user_receipt = Yii::$app->formatter->asTimestamp('now');
        return $orderTime->update() ? true : false;
    }

    /**
     * 更新用户平均时间和交易笔数
     * @param $order_id 订单编号
     */
    public static function updateUserScoringTime($order)
    {
        $order_time = AROrderTime::findOne(['order_id' => $order->id]);
        $u_scoring = ARUserScoring::findOne(['uid' => $order->uid]);
        $order_begin_tran = max($order_time->demander_confirm, $order_time->supplier_confirm, $order_time->broker_confirm);  //交易正式开始时间

        $count = AROrder::find()->where(['uid' => $order->uid,  'status' => AROrder::STATUS_SUCCESS])->count(); //作为需求方的次数
        $u_scoring->demander_confirm = ($u_scoring->demander_confirm * ($count - 1) + ($order_time->demander_confirm - $order_time->created_at)) / $count;  //需求方的平均响应时间
        $count = AROrder::find()->where(['uid' => $u_scoring->uid, 'type' => AROrder::TYPE_IN, 'status' => AROrder::STATUS_SUCCESS])->count(); //需求方作为买方的次数
        if($order->type == AROrder::TYPE_IN){   //买家
            $u_scoring->user_transfer = (($u_scoring->user_transfer * ($count - 1)) + $order_time->user_transfer - $order_begin_tran) / $count;
        }else{  //卖家
            $u_scoring->user_receipt = (($u_scoring->user_receipt * ($u_scoring->transaction_num - $count)  + $order_time->user_receipt - $order_time->broker_transfer)) / ($u_scoring->transaction_num + 1 - $count);
        }
        $u_scoring->transaction_num = $u_scoring->transaction_num + 1;
        $u_scoring->update();
        
        $count = AROrder::find()->where(['opp_uid' => $order->opp_uid, 'type' => AROrder::TYPE_IN, 'status' => AROrder::STATUS_SUCCESS])->count();  //供应方作为卖方的次数
        $opp_scoring = ARUserScoring::findOne(['uid' => $order->opp_uid]);
        if($order->type == AROrder::TYPE_IN){   //卖家
             $opp_scoring->user_receipt = (($opp_scoring->user_receipt * ($count - 1)) + $order_time->user_receipt - $order_time->broker_transfer) / $count;
        }else{  //买家
            $opp_scoring->user_transfer = (($opp_scoring->user_transfer * ($opp_scoring->transaction_num - $count)) + $order_time->user_transfer - $order_begin_tran) / ($opp_scoring->transaction_num + 1 - $count);
        }
        $opp_scoring->transaction_num = $opp_scoring->transaction_num + 1;
        $opp_scoring->update();
        
        if($order->broker != null){
            $broker_scoring = ARBrokerScoring::findOne(['uid' => $order->broker]);
            $broker_scoring->broker_transfer = (($broker_scoring->broker_transfer * $broker_scoring->transaction_num) + $order_time->broker_transfer - $order_time->broker_receipt) / ($broker_scoring->transaction_num + 1);
            $broker_scoring->broker_receipt =(($broker_scoring->broker_receipt * $broker_scoring->transaction_num) + $order_time->broker_receipt - $order_time->user_transfer) / ($broker_scoring->transaction_num + 1);
            $broker_scoring->transaction_num = $broker_scoring->transaction_num + 1;
            $broker_scoring->update();
        }
        return true;
    }
}
